import gtk, os, time
from GMM import utils

class text(gtk.EventBox):
    def __init__(self, string, projectfolder):
        gtk.EventBox.__init__(self)
        
        self.id=time.time()
        
        self.len=5
        self.comment=""
        self.text_effect=None
        self.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse("#BBBBBB"))
        
        self.projectfolder=projectfolder
        
        if string!="":
            data=string.split("#")
            #print string, data
            self.id=data[0]
            if len(data)>1: self.len=int(data[1])
            if len(data)>2: self.comment=data[2]
        
        self.photo=""
        self.thumbnail=""
        self.label=gtk.Label(self.comment)
        self.label.show()
        
        VBox=gtk.VBox()
        VBox.pack_end(self.label, False, False)
        
        self.image=gtk.Image()
        self.pixbuf = ""
        self.set_picture()
        
        self.set_pixbuf()
        VBox.pack_start(self.image)
        
        self.set_tooltip_text(self.comment)
        self.add(VBox)
        self.show_all()

    def set_len(self, lenght):
        self.len=lenght
    
    def set_width(self, width):
        self.set_size_request(width, 48)
        pass
    
    def get_frame(self):
        return self.photo, self.len
    
    def redraw_picture(self):
        print "redraw"
        #utils.execute(["convert" ,"-size", "800x600", "xc:transparent", "-fill", "black", "-draw", "rectangle 0,0,800,600", self.photo])
        #width=len(self.comment)*60
        utils.execute(["convert", "-size","800x600", "xc:transparent", "-channel", "RGBA", "-fill", "none", "-draw", "rectangle 0,0,800,600", "-gravity", "south", "-font", "Bookman-DemiItalic", "-pointsize", "72", "-draw", "text 25,60 '"+self.comment+"'", "-channel", "RGBA", "-gaussian", "0x6", "-fill", "white", "-stroke", "black", "-draw", "text 20,55 '"+self.comment+"'", "-resize", "800x600", self.photo])
        #utils.execute(["composite", "-gravity", "South", self.projectfolder +"temp-text.png" ,self.photo, self.photo])
        self.make_thumb(force=True)
        self.set_pixbuf()
        #"-channel", "RGBA", "-fill", "none", 
    
    def set_picture(self):
        try:
            os.mkdir(self.projectfolder + "text/")
        except: pass
        #photo_name=utils.get_name_no_ext(photo)
        self.photo=self.projectfolder + "text/" + str(self.id) + ".png"
        if (os.path.isfile(self.photo)==False):
            self.redraw_picture()
        self.make_thumb()
        
        #self.label.set_text(self.comment)
    
    def make_thumb(self, force=False):
        try:
            os.mkdir(self.projectfolder + "txt_thumb/")
        except: pass
        photo_name=utils.get_name_no_ext(self.photo)
        self.thumbnail=self.projectfolder + "txt_thumb/"+photo_name+".png"
        if os.path.isfile(self.thumbnail)==False or force:
            utils.execute(["convert", self.photo, "-resize", "200x200 ", self.thumbnail])
        
        self.pixbuf=gtk.gdk.pixbuf_new_from_file(self.thumbnail)
    
    def to_string(self):
        return str(self.id) + "#" + str(self.len) + "#" + self.comment
    
    def set_comment(self, comment):
        self.comment=comment
        self.label.set_text(comment)
        self.set_tooltip_text(self.comment)
    
    def set_pixbuf(self):
        width=self.pixbuf.get_width()
        height=self.pixbuf.get_height()
        
        if width > 100:
                height=height*100/width
                width=100
                if height > 48:
                    width=width*48/height
                    height=48
        pixbuf = self.pixbuf.scale_simple(width, height, gtk.gdk.INTERP_HYPER)
        self.image.set_from_pixbuf(pixbuf)